<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>敏感词检测与统计</title>
</head>
<body>

    <h2>敏感词检测与统计</h2>
    <label for="text">请输入文字：</label><br>
    <textarea id="text" rows="6" cols="50" placeholder="请输入文字进行监测..."></textarea><br><br>
    <button onclick="checkSensitiveWords()">检测敏感词</button>

    <p id="result"></p>

    <script>
        // 定义敏感词库
        const sensitiveWords = ["西南", "林业", "大学", "暴力", "政治", "国家"];

        function checkSensitiveWords() {
            var text = document.getElementById('text').value;
            var result = document.getElementById('result');
            var foundWords = {};
            var totalFound = false;

            // 遍历敏感词库，统计每个敏感词出现的次数
            sensitiveWords.forEach(function(word) {
                // 使用正则表达式找到所有匹配的敏感词
                var regex = new RegExp(word, "g");
                var matches = text.match(regex);
                
                if (matches) {
                    foundWords[word] = matches.length;  // 记录敏感词的出现次数
                    totalFound = true;
                }
            });

            // 判断是否找到敏感词，并输出结果
            if (totalFound) {
                var resultText = "警告：检测到敏感词：<br>";
                for (var word in foundWords) {
                    resultText += word + " 出现次数: " + foundWords[word] + " 次<br>";
                }
                result.innerHTML = resultText;
            } else {
                result.textContent = "未检测到敏感词。";
            }
        }
    </script>

</body>
</html>
